package pts.salooner.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.log4j.Logger;

public class DaoUtil {

	private final static Logger logger = Logger.getLogger(DaoUtil.class);

	private static DaoUtil singleton;

	private Connection connect = null;

	public static DaoUtil getDaoUtil() {
		if (singleton == null) {
			singleton = new DaoUtil();
		}
		return singleton;
	}

	public ResultSet executeSelectStatement(String query) {

		ResultSet resultSet = null;
		Statement statement = null;

		if (connect == null) {
			this.connect();
		}

		try {
			statement = connect.createStatement();
			resultSet = statement.executeQuery(query);

		} catch (SQLException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Query execution error", e);
			}
		}

		return resultSet;
	}

	public ResultSet executePreparedStatementQuery(String query) {
		ResultSet resultSet = null;
		PreparedStatement preparedStatement = null;

		if (connect == null) {
			this.connect();
		}

		try {
			if (connect != null) {
				preparedStatement = connect.prepareStatement(query);
				resultSet = preparedStatement.executeQuery();
			}

		} catch (SQLException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Query execution error in prepared statement", e);
			}
		}

		return resultSet;
	}

	public ResultSet executePreparedStatementQuery(String query,
			List<Object> args) {
		ResultSet resultSet = null;
		PreparedStatement preparedStatement = null;

		if (connect == null) {
			this.connect();
		}

		try {
			if (connect != null) {
				preparedStatement = connect.prepareStatement(query);

				for (int i = 0; i < args.size(); i++) {
					preparedStatement.setObject(i + 1, args.get(i));
				}

				resultSet = preparedStatement.executeQuery();
			}

		} catch (SQLException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Query execution error in prepared statement", e);
			}
		}

		return resultSet;
	}

	public ResultSet executePreparedStatementQuery(String query,
			List<Object> args, List<Integer> sqlType) {
		ResultSet resultSet = null;
		PreparedStatement preparedStatement = null;

		if (connect == null) {
			this.connect();
		}

		try {
			if (connect != null) {
				preparedStatement = connect.prepareStatement(query);

				for (int i = 0; i < args.size(); i++) {
					preparedStatement.setObject(i + 1, args.get(i),
							sqlType.get(i));
				}

				resultSet = preparedStatement.executeQuery();
			}

		} catch (SQLException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Query execution error in prepared statement", e);
			}
		}

		return resultSet;
	}

	public void executePreparedStatementUpdate(String query, List<Object> args,
			List<Integer> sqlType) {
		PreparedStatement preparedStatement = null;
		if (connect == null) {
			this.connect();
		}

		try {
			if (connect != null) {
				preparedStatement = connect.prepareStatement(query);

				for (int i = 0; i < args.size(); i++) {
					preparedStatement.setObject(i + 1, args.get(i),
							sqlType.get(i));
				}

				preparedStatement.executeUpdate();
			}

		} catch (SQLException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Query execution error in prepared statement", e);
			}
		}

	}

	private DaoUtil() {
		this.connect();
	}

	private void connect() {
		final DataConfig config = DataConfig.getDataConfig();
		final String jdbc_driver = config.getProperties("db_jdbc_driver");
		final String host = config.getProperties("db_host");
		final String username = config.getProperties("db_username");
		final String pwd = config.getProperties("db_pwd");

		try {
			Class.forName(jdbc_driver);
			connect = DriverManager.getConnection(host + "?user=" + username
					+ "&password=" + pwd);
		} catch (ClassNotFoundException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Driver not loaded", e);
			}
		} catch (SQLException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Startig connection error", e);
			}
		}
	}

	private void close() {
		// try {
		// if (connect != null) connect.close();
		//
		// } catch (SQLException e) {
		// if (logger.isDebugEnabled()) {
		// logger.debug("Closing connection error", e);
		// }
		// }
	}

}
